<?php
class dbbackMod extends commonMod
{
	public function __construct()
	{
		parent::__construct();
		}
	//显示备份
	public function get_table()
	{
		$db= new Dbbak($this->config['DB_HOST'],$this->config['DB_USER'],$this->config['DB_PWD'],$this->config['DB_NAME'],'utf8','../data/db_back/');
		$table=$db->getTables($this->config['DB_NAME']);
		echo __PUBLIC__;
		$this->assign('dbname',date("Ymd",time()));
		$this->assign('table',$table);

		$this->display('dbback/back');
		}
	//显示恢复
	public function show_rec()
	{
		$this->assign('list',$this->getFileName());
		$this->assign('lang',LANG::getPack('dbback'));
		$this->display('dbback/recovery.html');
		}
	//备份数据
	public function backup()
	{
		@set_time_limit(0);
		$db= new Dbbak($this->config['DB_HOST'],$this->config['DB_USER'],$this->config['DB_PWD'],$this->config['DB_NAME'],'utf8','../data/db_back/');
		$tb_name=$_POST['tbName'];
		$db_size=$_POST['dbsize'];
		$db_sql=$_POST['dbsql'];
		if($tb_name=="allback")
		{
			$data=$db->getTables($this->config['DB_NAME']);
			}
		else{
			$data=explode(",",substr($tb_name,0,strlen($tb_name)-1));
		}
		if($db->exportSql($data,$db_size,$db_sql))
		{echo 1;}else{echo 0;}
		}
	//获取备份文件列表
	public function getFileName()
	{
		$f_open="../data/db_back/";
		if(is_dir($f_open)){
		 $dir = opendir($f_open);
		 while(false!=$file=readdir($dir)){
			if($file!='.' && $file!='..' && !is_dir($f_open."\\".$file)){
				$file=$file;
				$arr_file1[]=$file;
			}
		}
		}
		closedir($dir);
		if($arr_file1)
		{
			$i=0;
			foreach($arr_file1 as &$val)
			{
				$path='../data/db_back/'.$val;
				$arr_file2[$i]['name']=$val;
				$arr_file2[$i]['size']=filesize($path);
				$arr_file2[$i]['time']=date("Y-m-d h:i:s",fileatime($path));
				$arr_file2[$i]['edition']=LANG::get('con_edition','');
				$i++;
				}
			}
		return $arr_file2;
		}
	//恢复已存在备份
	public function recovery()
	{
		@set_time_limit(0);
		$db= new Dbbak($this->config['DB_HOST'],$this->config['DB_USER'],$this->config['DB_PWD'],$this->config['DB_NAME'],'utf8','../data/db_back/');
		$path=$_GET['path'];
		if($db->importSql($path))
		{
			$this->success('数据恢复成功！','/show_rec');
			}
		else{
			$this->error('数据恢复失败！');
			}
		}
	//上传备份并恢复
	public function recovery_file()
	{
		@set_time_limit(0);
		if($_FILES['dbfile']['name']!="")
		{
			$uploadpath='../data/db_back'; //设置上传文件的路径
			$imgpath='data/db_back/'; //设置文件的路径
			if($imgupload=$this->_upload($uploadpath)){
			  $db= new Dbbak($this->config['DB_HOST'],$this->config['DB_USER'],$this->config['DB_PWD'],$this->config['DB_NAME'],'utf8','../data/db_back/');
			  if($db->importSql('../data/db_back/'.$imgupload['0']['savename']))
			  {
				  $this->success('数据恢复成功！','/show_rec');;
				  }
			  else{
				  $this->error('数据恢复失败！');
				  }
			}else{}
		}
		}
	public function alldel()
	{
		$file_name=$_POST['fileName'];
		$new_file = substr($file_name,4,strlen($file_name)-5);
		$db_file_name=explode(',',$new_file);
		foreach($db_file_name as $val)
		{
			if(is_file('../data/db_back/'.$val))
			{
			   unlink('../data/db_back/'.$val);
				}
			}
			echo 1;
			}
		}
?>